package com.seari.scheduling.plan;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.seari.Utils.DateUtils;
import com.seari.bean.PersonInfoBean;
import com.seari.bean.PostInfoBean;
import com.seari.bean.ReqPlanShiftBean;
import com.seari.service.PostInfoService;

@Component
public class PlanMonMidClass implements PlanScheduClass
{

	@Autowired
	PostInfoService postInfoService;

	private int more = 0;
	private int mire = 0;

	@Override
	public List<PersonInfoBean> planScheduling(ReqPlanShiftBean bean, List<PersonInfoBean> pesonInfo)
	{
		if (pesonInfo.size() != 4)
		{
			return pesonInfo;
		}
		Map<String, PersonInfoBean> per = new HashMap<>();
		List<String> onDuty = new ArrayList<>();
		List<PersonInfoBean> persons = new ArrayList<>();

		String postName = pesonInfo.get(0).getPostInfo();
		PostInfoBean postInfo = postInfoService.getPostInfo(postName);
		more = postInfo.getWeekMore(); // 早班补足量
		mire = postInfo.getWeekNire(); // 晚班补足量

		for (PersonInfoBean person : pesonInfo)
		{
			String id = person.getUid();
			onDuty.add(id);
			onDuty.add(id);
			per.put(id, person);
		}

		int days = 0;
		if (bean.getPlanShiftType().equals("customWeek"))
		{
			days = 7;
		} else
		{
			days = DateUtils.getDaysOfMonth(bean.getMonth()); // 获取当月天数
		}

		List<String> morningShift = new ArrayList<>(days);
		List<String> mightShift = new ArrayList<>(days);

		morningShift = morningShiftScheduling(more, days, onDuty);
		mightShift = nightShiftScheduling(mire, days, onDuty);

		postInfo.setPname(postName);
		postInfo.setWeekMore(more);
		postInfo.setWeekNire(mire);
		postInfoService.updatePostInfoBean(postInfo);

		String[] temp = new String[2];
		try
		{
			for (int i = 0; i < morningShift.size(); i++)
			{
				System.out.println("Start I :" + i);
				String pidMon = morningShift.get(i);
				PersonInfoBean personInfoMon = per.get(pidMon);
				personInfoMon.getShiftInfo().add(i, "早");
				temp[0] = pidMon;

				String pidMid = mightShift.get(i);
				PersonInfoBean personInfoMid = per.get(pidMid);
				personInfoMid.getShiftInfo().add(i, "中");
				temp[1] = pidMid;

				Arrays.sort(temp);

				for (String pid : per.keySet())
				{
					if (Arrays.binarySearch(temp, pid) < 0)
					{
						PersonInfoBean personInfo = per.get(pid);
						personInfo.getShiftInfo().add(i, "休");
					}
				}
				System.out.println("End I :" + i);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}

		Iterator<Entry<String, PersonInfoBean>> entries = per.entrySet().iterator();
		while (entries.hasNext())
		{
			Entry<String, PersonInfoBean> entry = entries.next();
			persons.add(entry.getValue());
		}

		return persons;
	}

	public List<String> morningShiftScheduling(int more, int days, List<String> onDuty)
	{

		List<String> morningShift = new ArrayList<>(days);
		int mormod = (days - more) % 6; // 补足的数量
		int moslice = (days - more) / 6;

		for (int i = more; i >= 1; i--)
		{
			morningShift.add(onDuty.get(onDuty.size() - i));
		}

		for (int i = 0; i < moslice; i++)
		{
			morningShift.addAll(onDuty);
		}

		for (int i = 0; i < mormod; i++)
		{
			morningShift.add(onDuty.get(i));
		}

		this.more = 6 - mormod;

		return morningShift;
	}

	public List<String> nightShiftScheduling(int mire, int days, List<String> onDuty)
	{

		List<String> mightShift = new ArrayList<>(days);
		int nigmod = (days - mire) % 6;
		int nislice = (days - mire) / 6;

		for (int i = mire; i >= 1; i--)
		{
			mightShift.add(onDuty.get(onDuty.size() - i));
		}

		for (int i = 0; i < nislice; i++)
		{
			mightShift.addAll(onDuty);
		}

		for (int i = 0; i < nigmod; i++)
		{
			mightShift.add(onDuty.get(i));
		}

		this.mire = 6 - nigmod;

		return mightShift;

	}
}
